package com.kuang.dao;

import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import javafx.print.Collation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Repository //这是做什么的?什么是被springboot托管
public class EmployeeDao {//员工Dao
    //模拟数据库中的数据
    private static Map<Integer, Employee> employees=null;
    //@Autowired     //这是做什么的?
    private  DepartmentDao departmentDao;
    //员工有所属的部门
    static {
        employees=new HashMap<Integer,Employee>();//创建一个员工
//        employees.put(101,new Employee(1001,"aa","a111@abc.com",0,departmentDao.getDepartmentById(101)));
//        employees.put(102,new Employee(1002,"bb","b111@abc.com",1,departmentDao.getDepartmentById(102)));
//        employees.put(103,new Employee(1003,"cc","c111@abc.com",0,departmentDao.getDepartmentById(103)));
//        employees.put(104,new Employee(1004,"dd","d111@abc.com",1,departmentDao.getDepartmentById(104)));
//        employees.put(105,new Employee(1005,"ee","e111@abc.com",0,departmentDao.getDepartmentById(105)));
        //101是id,1001是什么? 我的意思是哪个id对应数据库中的id?我感觉应该是1001.
    }
    private static Integer initId=1006;//主键自增
    public void save(Employee employee){//添加一个员工
        if(employee.getId()==null){
            employee.setId(initId++);//先取,后加
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));//外部需要一个数据库对象,数据库对象需要一个内存对象的id
        employees.put(employee.getId(),employee);//这就把新用户存进hashMap里了
    }
    public Collection<Employee> getAll(){//获取全部用户
        return employees.values();
    }
    public Employee getEmployeeById(Integer id){//查询一个用户
        return employees.get(id);
    }
    public void deleteEmployeeById(Integer id){//删除一个用户
        employees.remove(id);
    }
}
